package _51_构建乘积数组;
/*
题目描述
给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。
 */

/**
 * 思路：
 * 先算左面的
 * B[0] = 1;
 * B[1] = B[0] *a[0]    0
 * B[2] = B[1]*a[1]     0 1
 * B[3] = B[2]*a[2]     0 1 2
 * <p>
 * tmp = a[3]
 * b[2] = b[2] *tmp = 0 1 3   tmp
 */
public class Solution {
    public int[] multiply(int[] A) {
        int b[] = new int[A.length];
        b[0] = 1;
        for (int i = 1; i < A.length; i++) {
            b[i] = b[i - 1] * A[i - 1];
        }
        int tmp = A[A.length - 1];
        for (int i = A.length - 2; i >= 0; i--) {
            b[i] = b[i] * tmp;
            tmp = tmp * A[i];
        }
        return b;
    }
}
